/**
 * jims
 */
package com.jims.master.modules.drug.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.ChineseCharToEnUtils;
import com.jims.master.modules.drug.dao.DrugClassDictDao;
import com.jims.master.modules.drug.dao.DrugNameDictDao;
import com.jims.master.modules.drug.entity.DrugClassDict;
import com.jims.master.modules.drug.entity.DrugDict;
import com.jims.master.modules.drug.entity.DrugNameDict;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 药品名称字典Service
 *
 * @author 朱齐
 * @version 2017-03-01
 */
@Service
@Transactional(readOnly = true)
public class DrugNameDictService extends CrudService<DrugNameDictDao, DrugNameDict> {

    @Autowired
    private DrugClassDictDao drugClassDictDao;
    @Autowired
    private DrugInfoService drugInfoService;
    @Autowired
    private DrugPriceService drugPriceService;

    public DrugNameDict get(String id) {
        return super.get(id);
    }

    public List<DrugNameDict> findList(DrugNameDict drugNameDict) {
        return super.findList(drugNameDict);
    }

    public Page<DrugNameDict> findPage(Page<DrugNameDict> page, DrugNameDict drugNameDict) {
        return super.findPage(page, drugNameDict);
    }

    /**
     * 根据药品字典来过滤药品信息
     * @param page
     * @param drugNameDict
     * @return
     */
    public Page<DrugNameDict> findPageByDict(Page<DrugNameDict> page, DrugNameDict drugNameDict) {
//        drugNameDict.updateOrgId();
        drugNameDict.setPage(page);
        page.setList(dao.findPageByDict(drugNameDict));
        return page;
    }
    /*
    * @ZhuQ
    * 保存查询drugInfo中包含的drugNameDictlist
    * */
    public Page<DrugNameDict> findPageByInfo(Page<DrugNameDict> page, DrugNameDict drugNameDict) {
        drugNameDict.updateOrgId();
        drugNameDict.setPage(page);
        page.setList(dao.findListByInfo(drugNameDict));
        return page;
    }

    /*
    * @zhuq
    * 保存drug_name_dict
    * */
    @Transactional(readOnly = false)
    public int saveDrugName(DrugNameDict drugNameDict) {
        //后台将药品类型的id转换成编码
        if (drugNameDict.getDrugType() != null && !drugNameDict.getDrugType().equals("")) {
            DrugClassDict drugClassDict = drugClassDictDao.get(drugNameDict.getDrugType());
            drugNameDict.setDrugCode(drugClassDict.getClassCode());
        }

        String drugType = drugNameDict.getDrugCode();
        //由药品类型(不足6位补X)+三位序数号+剂型+00组成药品code
        for (int i = drugType.length(); i < 6; i++) {
            drugType = drugType + "X";
        }
        String codeNum = "";
        //获取药品序号
        String drugForm = drugNameDict.getDrugFrom();
        drugNameDict.setDrugFrom(drugForm + "00");
        int code = dao.findByCodeNo(drugNameDict);
        if (code < 10) {
            codeNum = "00" + code;
        } else if (code >= 10 && code < 100) {
            codeNum = "0" + code;
        } else {
            codeNum = "" + code;
        }
        drugNameDict.setDrugCode(drugType + codeNum + drugNameDict.getDrugFrom());
        drugNameDict.setInputCode(ChineseCharToEnUtils.getAllFirstLetter(drugNameDict.getDrugName()));
        drugNameDict.setDrugFrom(drugForm);
        return super.save(drugNameDict);
    }

    @Transactional(readOnly = false)
    public int save(DrugNameDict drugNameDict) {
        return super.save(drugNameDict);
    }

    @Transactional(readOnly = false)
    public int delete(DrugNameDict drugNameDict) {
        return super.delete(drugNameDict);
    }

    public DrugNameDict findByDrugCode(String drugCode) {
        return dao.findByDrugCode(drugCode);
    }
    /**
     * 加载药品列表
     * @param drugNameDict
     * @return
     * @author
     */
    public List<DrugNameDict> getDrugNameList(DrugNameDict drugNameDict) {
        return dao.getDrugNameList(drugNameDict);
    }

    @Transactional(readOnly = false)
    public int updateDrug(DrugDict drugDict) {
        DrugNameDict drugNameDict=new DrugNameDict();
        String drugCode=drugDict.getDrugCode();
        drugNameDict=dao.findByDrugCode(drugCode);
        drugNameDict.setDrugName(drugDict.getDrugName());
        drugNameDict.preUpdate();
        int j=save(drugNameDict);
        return j;
    }

    @Transactional(readOnly = false)
    public String saveDrugNameDictExcel() {
//        Workbook readwb = null;
//        try
//        {
//            //构建Workbook对象, 只读Workbook对象
//            //直接从本地文件创建Workbook
//            FileInputStream instream = new FileInputStream("C:/Users/zhu/Desktop/整理后数据/drug_name_dict.xls");
//            readwb = Workbook.getWorkbook(instream);
//            //Sheet的下标是从0开始
//            //获取第一张Sheet表
//            Sheet readsheet = readwb.getSheet(0);
//            //获取Sheet表中所包含的总列数
//            int rsColumns = readsheet.getColumns();
//            //获取Sheet表中所包含的总行数
//            int rsRows = readsheet.getRows();
//            //获取指定单元格的对象引用
//
//            for (int i = 0; i < rsRows; i++)
//            {
//                DrugNameDict drugNameDict=new DrugNameDict();
//                for (int j = 0; j < rsColumns; j++)
//                {
//                    if(j==0){
//                        Cell cell = readsheet.getCell(j, i);
//                        drugNameDict.setDrugType(cell.getContents());
//                    }
//                    if(j==1){
//                        Cell cell = readsheet.getCell(j, i);
//                        drugNameDict.setDrugName(cell.getContents());
//                    }
//                    if(j==2){
//                        Cell cell = readsheet.getCell(j, i);
//                        drugNameDict.setDrugFrom(cell.getContents());
//                    }
//                    drugNameDict.setDrugIndicator("1");
//
//
//                }
//                saveDrugName(drugNameDict);
//                System.out.println(drugNameDict.getDrugName());
//            }
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            if(readwb!=null){
//                readwb.close();
//            }
//        }

        return "";
    }


    @Transactional(readOnly = false)
    public String saveInfoPriceExcel() {
//        Workbook readwb = null;
//        try
//        {
//            //构建Workbook对象, 只读Workbook对象
//            //直接从本地文件创建Workbook
//            FileInputStream instream = new FileInputStream("C:/Users/zhu/Desktop/整理后数据/drug_name_dict.xls");
//            readwb = Workbook.getWorkbook(instream);
//            //Sheet的下标是从0开始
//            //获取第一张Sheet表
//            Sheet readsheet = readwb.getSheet(0);
//            //获取Sheet表中所包含的总列数
//            int rsColumns = readsheet.getColumns();
//            //获取Sheet表中所包含的总行数
//            int rsRows = readsheet.getRows();
//            //获取指定单元格的对象引用
//
//            for (int i = 1; i < rsRows; i++)
//            {
//                DrugInfo drugInfo=new DrugInfo();
//                DrugPrice drugPrice =new DrugPrice();
//                for (int j = 0; j < rsColumns; j++)
//                {
//                    if(j==0){
//                        Cell cell = readsheet.getCell(j, i);
//                        DrugDict drugDict = new DrugDict();
//                        drugDict.setId(cell.getContents());
//                        drugInfo.setDrugDict(drugDict);
//                    }
//                    if(j==2){
//                        Cell cell = readsheet.getCell(j, i);
//                        drugInfo.setPackageUnit(cell.getContents());
//                    }
//                    if(j==4){
//                        Cell cell = readsheet.getCell(j, i);
//                        drugInfo.setMinSpec(cell.getContents());
//                    }
//                    if(j==5){
//                        Cell cell = readsheet.getCell(j, i);
//                        drugInfo.setMinUnits(cell.getContents());
//                    }
//                    if(j==6){
//                        Cell cell = readsheet.getCell(j, i);
//                        drugInfo.setPackageNum(Double.valueOf(cell.getContents()));
//                    }
//                    if(j==7){
//                        Cell cell = readsheet.getCell(j, i);
//                        drugInfo.setPassNo(cell.getContents());
//                    }
//                    if(j==8){
//                        Cell cell = readsheet.getCell(j, i);
//                        drugPrice.setTradePrice(Double.valueOf(cell.getContents()));
//                    }
//                    if(j==9){
//                        Cell cell = readsheet.getCell(j, i);
//                        drugPrice.setRetailPrice(Double.valueOf(cell.getContents()));
//                    }
//                    if(j==10){
//                        Cell cell = readsheet.getCell(j, i);
//                        drugPrice.setPriceMax(Double.valueOf(cell.getContents()));
//                    }
//                    if(j==11){
//                        DrugSupplierCata drugSupplierCata=new DrugSupplierCata();
//                        Cell cell = readsheet.getCell(j, i);
//                        drugSupplierCata.setId(cell.getContents());
//                        drugInfo.setDrugSupplierCata(drugSupplierCata);
//                    }
//
//
//                }
//                drugInfo.setGmp("0");
//                drugInfo.setPackageSpec(drugInfo.getMinSpec()+"*"+drugInfo.getPackageNum());
//                drugInfoService.save(drugInfo);
//
//                drugPrice.setStartDate(new Date());
//                drugPrice.setClassOnRcpt("J");
//                drugPrice.setClassOnReckoning("P1");
//                drugPrice.setSubjCode("P1");
//                drugPrice.setClassOnMr("13");
//                drugPrice.setPackageSpec(drugInfo.getPackageSpec());
//                drugPrice.setUnits(drugInfo.getPackageUnit());
//                drugPrice.setDrugInfo(drugInfo);
//
//                drugPriceService.savePrice(drugPrice);
//            }
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            if(readwb!=null){
//                readwb.close();
//            }
//        }
        return "";
    }

}